package com.changgou.order.service.impl;


import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CartServiceImpl implements CartService {

    private static final String CART="cart_";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    /**
     *      添加购物车
     * @param skuId     商品id
     * @param num       商品数量
     * @param username     是谁在添加购物车
     */
    @Override
    public void addCart(String skuId, Integer num, String username) {

        //1.查询redis中相对于的商品信息
        //通过redis中的hash格式
        //orderItem   代表了每个购物项
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART+username).get(skuId);

        if (orderItem != null){
            if (orderItem.getNum()<=0){

                //数量为0删除该商品
                redisTemplate.boundHashOps(CART+username).delete(skuId);
                return;
            }

            //2.如果当前商品在redis中存在，则更新商品的数量和价钱     存在

            //跟新商品的数量
            orderItem.setNum(orderItem.getNum()+num);
            //跟新商品的价格
            orderItem.setMoney(orderItem.getNum()*orderItem.getPrice());
            //跟新需要付的总价钱
            orderItem.setPayMoney(orderItem.getNum()*orderItem.getPrice());
        }else {

            //3.如果当前商品在redis中不存在，将商品添加到redis中       不存在
            Sku sku = skuFeign.findById(skuId).getData();

            Spu spu = spuFeign.findSpuById(sku.getSpuId()).getData();

            //封装orderItem 并  保存到redis中
        orderItem =   this.sku2OrderItem(sku,spu,num);

        }


        //3.将orderItem 添加到redis中
        redisTemplate.boundHashOps(CART+username).put(skuId,orderItem);



    }

    /**
     *  查询购物车列表数据
     *
     * @param username
     * @return
     */
    @Override
    public Map list(String username) {
        Map map = new HashMap();
        //封装数据

        //获取到封装数据的order集合
        List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART + username).values();
        map.put("orderItemList",orderItemList);

        //商品的总数量与总价格

        Integer totalNum = 0 ;
        Integer totalMoney = 0 ;

        for (OrderItem orderItem : orderItemList) {
            totalNum = orderItem.getNum();

            totalMoney=orderItem.getMoney();

        }

        map.put("totalNum",totalNum);
        map.put("totalMoney",totalMoney);
        return map;
    }

    /**
     *      返回一个orderitem对象  用于封装数据的
     * @param sku
     * @param spu
     * @param num
     * @return
     */
    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem = new OrderItem();
        //spuId
        orderItem.setSpuId(sku.getSpuId());
        //skuId
        orderItem.setSkuId(sku.getId());
        //商品名称
        orderItem.setName(sku.getName());
        //商品单价
        orderItem.setPrice(sku.getPrice());
        //商品数量
        orderItem.setNum(num);
        //商品总价
        orderItem.setMoney(orderItem.getPrice()*num);
        //商品支付金额
        orderItem.setPayMoney(orderItem.getPrice()*num);
        //商品图片
        orderItem.setImage(sku.getImage());
        //商品总重量
        orderItem.setWeight(sku.getWeight()*num);
        //商品的分类信息
        orderItem.setCategoryId1(spu.getCategory1Id());

        orderItem.setCategoryId2(spu.getCategory2Id());

        orderItem.setCategoryId3(spu.getCategory3Id());




        return orderItem;

    }
}
